package com.grts.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;




import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.grts.pojo.CourseBean;
import com.grts.pojo.MajorBean;
import com.grts.pojo.Teacher;
import com.grts.pojo.t_Major_CourseBeanKey;
import com.grts.service.ICourseService;
import com.grts.service.IMajorService;
import com.grts.service.It_Major_CourseBeanService;
import com.grts.service.Teacherservice;
import com.grts.utils.PageModel;


@Controller
@RequestMapping("/course")
public class CourseController {
	@Resource
	private ICourseService sc;
	@Resource
	private Teacherservice st;
	@Resource
	private IMajorService sm;
	@Resource
	private It_Major_CourseBeanService mc;
	
	private PageModel pm = new PageModel();
	
	
	@RequestMapping("/toUpdate")
	public String queryById(Map m,@RequestParam(name="cid",required=false,defaultValue="1")int cid){
		CourseBean cour = sc.selectByPrimaryKey(cid);
		 List<Teacher> tlist = st.queryall(null);
		 List<MajorBean> mlist = sm.queryAll();
		 m.put("majors", mlist);
		 m.put("teachers", tlist);
		m.put("cc",cour);
		return "courseAddOrUpdate";
	}
	
	
	@RequestMapping("/queryByPage")
	public String queryByPage(Map m,HttpServletRequest req){
		CourseBean cour = new CourseBean();
		//获得请求中的所有参数名:
		java.util.Enumeration en = req.getParameterNames();
		if(!en.hasMoreElements()){  
		//请求为空 执行
		List<CourseBean> clist = sc.queryAll(null);
		pm.setCurrentPage(1);pm.setPageSize(5);
		pm.setTotalCount(clist.size());	
		m.put("cours", clist);		
		}else{	
		//所有请求不为空 执行
		String cname = req.getParameter("cname").trim();
		cour.setCname(cname); 
		List<CourseBean> qlist = sc.queryAll(cour);
		pm.setTotalCount(qlist.size());
		if(!("".equals(req.getParameter("currentPage").trim()) && req.getParameter("currentPage").trim() == null)){
			int currentPage =Integer.parseInt(req.getParameter("currentPage").trim());
			int pageSize = Integer.parseInt(req.getParameter("pageSize").trim());		
			cour.setCurrentPage((currentPage - 1) * pageSize); cour.setPageSize(pageSize);
			pm.setCurrentPage(currentPage); pm.setPageSize(pageSize);				
		}else{
			cour.setCurrentPage((pm.getCurrentPage()-1)*pm.getPageSize());
			cour.setPageSize(pm.getPageSize());
		}
		List<CourseBean> clist =  sc.queryByPage(cour);
		m.put("cours", clist);
		}
		m.put("pageModel", pm);		
		return "course";
	}
	
	@RequestMapping("/AddOrUpdate")
	public String AddOrUpdate(HttpServletRequest req){
		CourseBean cour = new CourseBean();
		Integer cid =  Integer.valueOf(req.getParameter("cid").trim());
		 cour.setCid(cid);
		 cour.setCname(req.getParameter("cname").trim());
		 cour.setCdetails(req.getParameter("cdetails").trim());
		 cour.setCteacherid(Integer.parseInt(req.getParameter("cteacherid").trim()));
			 
	 	if(cid != 0){
	 		sc.updateByPrimaryKey(cour);
	 		t_Major_CourseBeanKey major_course = new t_Major_CourseBeanKey();
			 major_course.setCid(cid);
			mc.deleteAllBycid(major_course);
			if(req.getParameterValues("checkboxname") != null){
	 		String[] mids = req.getParameterValues("checkboxname"); 		
			 for(int i=0;i<mids.length;i++){
		// t_Major_CourseBeanKey[] major_course = new t_Major_CourseBeanKey[mids.length];
				 int mid = Integer.parseInt(mids[i].trim());
				 System.out.println("专业编号："+mid);
				 mc.insert2(mid,cid);			
			 }
	 		}
		}else{
			sc.insertSelective(cour);
		// System.out.println("主键ID值："+cour.getCid());
			if(req.getParameterValues("checkboxname") != null){
		 		String[] mids = req.getParameterValues("checkboxname"); 		
				 for(int i=0;i<mids.length;i++){
					 int mid = Integer.parseInt(mids[i].trim());
					 mc.insert2(mid,cour.getCid());			
				 }
			}
		}
		return "redirect:queryByPage";
	}
	
	@RequestMapping("/toAdd")
	public String init(Map m){
		List<Teacher> tlist = st.queryall(null);
		List<MajorBean> mlist = sm.queryAll();
		m.put("majors", mlist);
		m.put("teachers", tlist);
		return "courseAddOrUpdate";
	}
	
	@RequestMapping("/delete")
	public String delete( @RequestParam(name="cid",required=false,defaultValue="-1")Integer cid){
	//	System.out.println(cid);
		 sc.deleteByPrimaryKey(cid);
		 t_Major_CourseBeanKey major_course = new t_Major_CourseBeanKey();
		 major_course.setCid(cid);
		mc.deleteAllBycid(major_course);
		return "redirect:queryByPage";
	}
	
/*
	@RequestMapping("/ajax")
	public String ajax(CourseBean u,HttpServletResponse resp) throws IOException{
		System.out.println("jquery方式的ajax数据"+u);
		PrintWriter out = resp.getWriter();
		out.write("OK...");
		out.flush();
		out.close();
		return "redirect:queryById";
	}	
	
	@RequestMapping("/ajax2")
	public void ajax2(CourseBean u,HttpServletResponse resp) throws IOException{
		System.out.println("get方式的ajax数据"+u);
		PrintWriter out = resp.getWriter();
		out.write("OK...");
		out.flush();
		out.close();

	}	
	
	@RequestMapping("/login")
	public void queryall(CourseBean u,HttpServletResponse response,HttpServletRequest request ) throws IOException{
	PrintWriter out = response.getWriter();
//	ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");	
//	sc = (IUserService1) ac.getBean("userServiceImpl1");
	List<CourseBean> ulist = sc.queryAll(u);
		System.out.println(ulist.get(0));
		if(ulist != null && ulist.size() > 0){
			out.write("ok");
		}else{
			//登录失败
			out.append("error");
		}
		out.flush();
		out.close();
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public Map<String,Object> query(CourseBean user){
		System.out.println("测试数据");
		List<CourseBean> list = sc.queryAll(null);
		for(CourseBean u : list){
			System.out.println(u);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total",list.size());
		map.put("rows", list);
		return map;
	}*/
}
